package com.heyqing.divide;

/**
 * ClassName:Test1
 * Package:com.heyqing.divide
 * Description:
 *
 * @Date:2024/6/18
 * @Author:Heyqing
 */
import java.util.Arrays;
import java.util.Scanner;

public class Test1 {
    static int size;//棋盘大小
    static int[][] arr;//表示棋盘
    static int count = 1;//定义一个静态count用于填充

    public static void main(String[] args) {
        init();
        Cover(0,0,0,0,size);
        Output(size);
    }
    private static void init(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入棋盘大小：");
        int size = sc.nextInt();
        arr = new int[size][size];
        System.out.println("请输入棋子，0为特殊棋子");
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print("请输入第"+(i+1)+"行，第"+(j+1)+"列棋子：");
                int b = sc.nextInt();
                arr[i][j] = b;
            }
        }
        sc.close();
    }
    //4.定义解决问题的函数
    public static void Cover(int leftRow,int leftCol,int specialRow,int specialCol,int size){
        //出口
        if(size < 2){
            return;
        }
        count++;
        int flag = count;
        //如果特殊方块在左上子棋盘
        if(specialRow < leftRow + size/2 && specialCol < leftCol + size/2){
            //递归覆盖该子棋盘
            Cover(leftRow,leftCol,specialRow,specialCol,size/2);
            //使得剩余的三个子棋盘性质与原问题相同
            arr[leftRow + size/2 - 1][leftCol + size/2] = flag;//填充右上子棋盘左下角
            arr[leftRow + size/2][leftCol + size/2 - 1] = flag;//填充左下子棋盘右上角
            arr[leftRow + size/2][leftCol + size/2] = flag;//填充右下棋盘左上角
            //递归覆盖三个子棋盘
            Cover(leftRow,leftCol+size/2,leftRow + size/2 - 1, leftCol + size/2,size/2);//右上子棋盘
            Cover(leftRow + size/2,leftCol,leftRow + size/2,leftCol + size/2 - 1,size/2);//左下子棋盘
            Cover(leftRow + size/2,leftCol + size/2,leftRow + size/2,leftCol + size/2,size/2);//右下子棋盘
        }else if(specialRow < leftRow + size/2 && specialCol >= leftCol + size/2){//如果特殊方块在右上子棋盘
            Cover(leftRow,leftCol + size/2,specialRow,specialCol,size/2);
            arr[leftRow + size/2 - 1][leftCol + size/2 - 1] = flag;//填充左上子棋盘右下角
            arr[leftRow + size/2][leftCol + size/2 - 1] = flag;//填充左下子棋盘右上角
            arr[leftRow + size/2][leftCol + size/2] = flag;//填充右下棋盘左上角
            Cover(leftRow,leftCol,leftRow + size/2 - 1, leftCol + size/2 - 1,size/2);//左上子棋盘
            Cover(leftRow + size/2,leftCol,leftRow + size/2,leftCol + size/2 - 1,size/2);//左下子棋盘
            Cover(leftRow + size/2,leftCol + size/2,leftRow + size/2,leftCol + size/2,size/2);//右下子棋盘
        }else if(specialRow >= leftRow + size/2 && specialCol < leftCol + size/2){//如果特殊方块在左下子棋盘
            Cover(leftRow + size/2,leftCol,specialRow,specialCol,size/2);
            arr[leftRow + size/2 - 1][leftCol + size/2 - 1] = flag;//填充左上子棋盘右下角
            arr[leftRow + size/2 - 1][leftCol + size/2] = flag;//填充右上子棋盘左下角
            arr[leftRow + size/2][leftCol + size/2] = flag;//填充右下棋盘左上角
            Cover(leftRow,leftCol,leftRow + size/2 - 1, leftCol + size/2 - 1,size/2);//左上子棋盘
            Cover(leftRow,leftCol+size/2,leftRow + size/2 - 1, leftCol + size/2,size/2);//右上子棋盘
            Cover(leftRow + size/2,leftCol + size/2,leftRow + size/2,leftCol + size/2,size/2);//右下子棋盘
        }else if(specialRow >= leftRow + size/2 && specialCol >= leftCol + size/2){//如果特殊方块在右下子棋盘
            Cover(leftRow + size/2,leftCol + size/2,specialRow,specialCol,size/2);
            arr[leftRow + size/2 - 1][leftCol + size/2 - 1] = flag;//填充左上子棋盘左下角
            arr[leftRow + size/2 - 1][leftCol + size/2] = flag;//填充右上子棋盘左下角
            arr[leftRow + size/2][leftCol + size/2 - 1] = flag;//填充左下子棋盘右上角
            Cover(leftRow,leftCol+size/2,leftRow + size/2 - 1, leftCol + size/2,size/2);//右上子棋盘
            Cover(leftRow + size/2,leftCol,leftRow + size/2,leftCol + size/2 - 1,size/2);//左下子棋盘
            Cover(leftRow + size/2,leftCol + size/2,leftRow + size/2,leftCol + size/2,size/2);//右下子棋盘
        }

    }

    public static void Output(int size){
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println("");
        }
    }
}